package com.imooc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据库操作
 */
@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;
    /**
     * 查询列表
     * @return
     */
    @GetMapping(value="/girls")
    public List<Girl> girlList(){
        return girlRepository.findAll();
    }

    /**
     *添加
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping(value = "/addGirl")
    public  Girl girlAdd(@RequestParam("cupSize") String cupSize,@RequestParam("age") Integer age){
        Girl girl=new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    /**
     * 查找一个
     * @param id
     * @return
     */
    @GetMapping(value = "/selectGirl/{id}")
    public Girl selectGirl(@PathVariable("id") Integer id){
        return girlRepository.findOne(id);
    }
    /**
     * 修改
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping(value = "/updateGirl/{id}")
    public Girl updateGirl(@PathVariable("id") Integer id,@RequestParam("cupSize") String cupSize,@RequestParam("age") Integer age){
        Girl girl=new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return  girlRepository.save(girl);
    }

    /**
     * 删除
     * @param id
     */
    @PostMapping(value = "/deleteGirl/{id}")
    public void deleteGirl(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

    /**
     * 根据条件查询列表
     * @param age
     * @return
     */
    @GetMapping(value = "/selectGirlByAge/{age}")
    public List<Girl> selectGirlByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     * 事务管理
     */
    @PostMapping(value = "/addTwoGirl")
    public void addTwoGirl(){
        girlService.addTwoGirl();
    }
}
